package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.DelArr;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.sysLog.LogStringUtil;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.CommunicationProtocol;
import com.fzdy.dataplatform.entity.Product;
import com.fzdy.dataplatform.entity.User;
import com.fzdy.dataplatform.service.CommunicationProtocolService;
import com.fzdy.dataplatform.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/product")
public class ProductController extends BaseController {

    @Autowired
    private CommunicationProtocolService communicationProtocolService;
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/getproductlist")
    public String getproductlist(@RequestBody Page<Product> page) {
        Product product =new Product();
        product.setName(page.getName());
        productService.getPageByExampleLike(product,page);
        AjaxCallback ajaxCallback=new AjaxCallback(page.getList(), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/getAllProduct")
    public String getAllProduct() {
        List<Product> products = productService.getList();
        AjaxCallback ajaxCallback=new AjaxCallback(ModuleCallback.SUCCESSNO,"请求成功");
        ajaxCallback.setList(products);
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/editProduct")
    public String editProduct(@RequestBody Product product, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        User user = webUtilify.LoginUserinfo(request);
        /** 日志*/
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.PRODUCT, LogStringUtil.SUCCESS, user.getId());
        JSONObject log_content = new JSONObject();
        if (Check.checkObjNull(product.getCommunicationprotocolid())){
            ajaxCallback=new AjaxCallback(ModuleCallback.FAILURENO,"通信协议不能为空");
            log_sysLogApi.setStatus(LogStringUtil.FAILURE);
            log_content.put(LogStringUtil.CAUSEFAIL,"通信协议不能为空");
        }else if (Check.checkObjNull(product.getName())){
            ajaxCallback=new AjaxCallback(ModuleCallback.SUCCESSNO,"产品名不能为空");
            log_sysLogApi.setStatus(LogStringUtil.FAILURE);
            log_content.put(LogStringUtil.CAUSEFAIL,"产品名不能为空");
        }else if (Check.checkObjNull(product.getPno())){
            ajaxCallback=new AjaxCallback(ModuleCallback.FAILURENO,"产品编号不能为空");
            log_sysLogApi.setStatus(LogStringUtil.FAILURE);
            log_content.put(LogStringUtil.CAUSEFAIL,"产品编号不能为空");
        }else {
            if (product.getId()==null){
                //添加
                log_sysLogApi.setOperation(LogStringUtil.ADD);
                productService.insertT(product);
                ajaxCallback=new AjaxCallback(ModuleCallback.SUCCESSNO,"添加成功");
                log_content.put("产品信息",JSON.toJSONString(product));
            }else {
                
                log_sysLogApi.setOperation(LogStringUtil.UPDATA);
                Product oldProduct = productService.get(product.getId());
                log_content.put(LogStringUtil.BEFOREEDIT,JSON.toJSONString(oldProduct));
                if (Check.checkObjNull(oldProduct)){
                    ajaxCallback=new AjaxCallback(ModuleCallback.FAILURENO,"修改的产品不存在");
                    log_sysLogApi.setStatus(LogStringUtil.FAILURE);
                    log_content.put(LogStringUtil.CAUSEFAIL,"产品名不能为空");
                }else {
                    log_content.put(LogStringUtil.BEFOREEDIT,JSON.toJSONString(oldProduct));
                    oldProduct.setName(product.getName());
                    oldProduct.setPno(product.getPno());
                    oldProduct.setCommunicationprotocolid(product.getCommunicationprotocolid());
                    oldProduct.setMemo(product.getMemo());
                    log_content.put(LogStringUtil.AFTEREDIT,JSON.toJSONString(oldProduct));
                    productService.updateT(oldProduct);
                    ajaxCallback=new AjaxCallback(ModuleCallback.SUCCESSNO,"修改成功");
                }
            }
        }
        log_sysLogApi.setContent(log_content);
        syslogService.WriteLog(log_sysLogApi);
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/getProduct")
    public String getProduct(@RequestBody Product product) {
        product =productService.get(product.getId());
        AjaxCallback ajaxCallback=new AjaxCallback(ModuleCallback.SUCCESSNO, ModuleCallback.REQUESTSUCCESS);
        ajaxCallback.setData(product);
        System.out.println(JSON.toJSONString(product));
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/getcommunicationprotocollist")
    public String getcommunicationprotocollist() {
        List<CommunicationProtocol> list = communicationProtocolService.getList();
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功");
        ajaxCallback.setList(list);
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/delProduct")
    public String delProduct(@RequestBody DelArr delArr, HttpServletRequest request) {
        productService.batchDelete(delArr.getIds());
        JSONObject log_content = new JSONObject();
        log_content.put("id", delArr.getIds());
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.DEL, LogStringUtil.PRODUCT, LogStringUtil.SUCCESS,
                log_content, webUtilify.LoginUserinfo(request).getId());
        syslogService.WriteLog(log_sysLogApi);
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "删除成功");
        return JSON.toJSONString(ajaxCallback);
    }
}
